layui.use(["jquery", "upload", "form", "layer", "element", "laydate"], function () {
    var $ = layui.$,
        element = layui.element,
        layer = layui.layer,
        upload = layui.upload,
        form = layui.form;
        laydate  = layui.laydate;
    var s = "";
    var user = JSON.parse(localStorage.getItem('user'));
    $('input[name = createId]').val(user.userId);
    $('#test1').on("click",function () {
        $('#showBtn').html('<div class="layui-upload-list" id="demo2" style="margin-left: 120px"></div>');
    })



    //上传
    var i = 0;
    var uploadInst = upload.render({
        elem: '#test1'
        ,url: '/Product/upload'
        ,accept: 'file'
        ,field:'fil'
        ,bindAction:'#subUpload'
        ,auto:false
        , size: 1000
        , multiple: false
        , number: 9
        ,choose:function (obj) {
            var files = obj.pushFile();
            obj.preview(function(index, file, result){
                $('#demo2').append('<img src="' + result
                    + '" alt="' + file.name
                    + '"height="100px" width="100px" border="1px solid #ccc" class="layui-upload-img uploadImgPreView">');
            });
        }
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                // $('#demo1').attr('src', result); //图片链接（base64）

            });
            //layer.msg('上传中', {icon: 16, time: 0});
        }
        , done: function (res) {
            if(i<1){
                var path = res.data.split("static");
                $('input[name = acatarUrl]').val(path[1]);

                var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
                var temp = date.substring(8, -date.lastIndexOf('-') + 7);
                date = date.substr(date.lastIndexOf('-') + 1);
                date = temp + date
                $('input[name = registerDate]').val(date);

                $('#sub').click();
                i++;
            }else{
                i = 0;
            }
            if (s == "") {
                s = JSON.stringify(res);
            } else {
                s = s + "," + JSON.stringify(res);
            };
            localStorage.setItem("address1", s);
        }
        , error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }, progress: function (n, elem, e) {
            element.progress('demo', n + '%');
            if (n == 100) {
                //layer.msg('上传完毕', {icon: 1});
            }
        }
    });
    //返回
    backOff = function backOff() {
        location.href="authority-backstage.html";
    }

    //查询部门
    findDeptName = function findDeptName(){
        layer.open({
            type:2,
            content:"/authority/layer-addUser-dept.html",
            area:['670px','500px'],

        });
    }

    //性别
    form.on('radio(sex)', function(data){
        $('#sex').val(data.value);
    });
    //是否可用优惠卷
    form.on('radio(enabled)', function(data){
        $('#enabled').val(data.value);
    });
    //服务保证
    form.on('radio(status)', function(data){
        $('#status').val(data.value);
    });

    //设置日期
    laydate.render({
        elem: '#birthDate'
        ,type: 'datetime'
    });


    form.on('submit(formDemo)', function(data){
        $.ajax({
            url:'/User/addUser',
            type: 'post',
            dataType:'json',
            data:data.field,
            success:function (data) {
                if(data.data){
                  /*  //添加的操作日志
                    $.ajax({
                        url:'/Product/addProductOperationRecord',
                        dataType:'json',
                        data:{
                            "dictValue":"添加反馈日志","cssClass":"添加","createBy":$('#proId',window.parent.document).val(),
                            "remark":"无内容"
                        }
                    });*/
                    layer.msg("添加成功");
                }else{
                    layer.msg("添加失败");
                }
            }
        });
        return false;
    });
});